import { listUnit, pageUnit } from '@/api/unit'
import { listBrand } from '@/api/brand'

const state = {
  units: [],
  unitDir:[]
}

const mutations = {

  PAGE_UNIT: (state, data) => {
    state.units = data
  },
  LIST_UNIT:(state, data) => {
    state.unitDir = data
  }
}

const actions = {
  listUnit({ commit, state }) {
    return new Promise((resolve, reject) => {
      listUnit().then(data=> {
        const result = data.map(n => {
          return {
            label: n.name,
            value: n.id
          }
        })
        commit('LIST_UNIT',result)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  pageUnit({ commit, state },_data) {
    return new Promise((resolve, reject) => {
      pageUnit(_data).then(data=> {
        commit('PAGE_UNIT',data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

